//
//  JLStrokeLabel.swift
//  JLUIKit
//
//  Created by Jiang Chencheng on 2024/8/14.
//

import UIKit

public class JLStrokeLabel: UILabel {

    public var strokeWidth: CGFloat = 1
    
    public var strokeColor: UIColor?
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override func drawText(in rect: CGRect) {
        guard let strokeColor = strokeColor else {
            super.drawText(in: rect)
            return
        }

        let context = UIGraphicsGetCurrentContext()
        // 描边宽度
        context?.setLineWidth(strokeWidth)
        context?.setLineJoin(.round)
        context?.setTextDrawingMode(.stroke)

        let tempColor = textColor
        // 描边颜色
        textColor = strokeColor
        super.drawText(in: rect)
        
        // 文本颜色
        textColor = tempColor
        context?.setTextDrawingMode(.fill)
        super.drawText(in: rect)
    }
    
}


